package com.github.catvod.spider.merge;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;

/* loaded from: classes.dex */
public class O {
    private static PublicKey BQ(String str) {
        return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(aF.h(str)));
    }

    public static String I(String str, String str2) {
        try {
            PublicKey BQ = BQ(str2);
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(1, BQ);
            return new String(aF.V0(cipher.doFinal(str.getBytes("UTF-8"))));
        } catch (Exception e) {
            System.out.println("sadsad4" + e);
            return null;
        }
    }

    public static String V0(String str, String str2) {
        PublicKey BQ = BQ(str);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(2, BQ);
        return new String(cipher.doFinal(aF.h(str2)));
    }

    public static String h(String str, String str2) {
        byte[] bArr;
        PrivateKey oC = oC(str2);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(2, oC);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(aF.h(str));
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr2 = new byte[256];
        while (true) {
            int read = byteArrayInputStream.read(bArr2);
            if (read == -1) {
                return new String(byteArrayOutputStream.toByteArray());
            }
            if (256 == read) {
                bArr = bArr2;
            } else {
                bArr = new byte[read];
                for (int i = 0; i < read; i++) {
                    bArr[i] = bArr2[i];
                }
            }
            byteArrayOutputStream.write(cipher.doFinal(bArr));
        }
    }

    public static String j(String str, String str2) {
        String str3 = "error";
        byte[] h = aF.h(str2);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            PublicKey BQ = BQ(str);
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(2, BQ);
            int length = h.length;
            int i = 0;
            int i2 = 0;
            while (true) {
                int i3 = length - i;
                if (i3 <= 0) {
                    break;
                }
                byte[] doFinal = i3 > 128 ? cipher.doFinal(h, i, 128) : cipher.doFinal(h, i, i3);
                byteArrayOutputStream.write(doFinal, 0, doFinal.length);
                i2++;
                i = i2 * 128;
            }
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.close();
            if (byteArray == null) {
                return "error";
            }
            str3 = new String(byteArray);
            return str3;
        } catch (Exception unused) {
            return str3;
        }
    }

    public static PrivateKey oC(String str) {
        return KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(aF.h(str)));
    }

    public static String q(String str, String str2) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(1, KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(aF.h(str))));
            int length = str2.getBytes().length;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int i = 0;
            int i2 = 0;
            while (true) {
                int i3 = length - i;
                if (i3 <= 0) {
                    byte[] byteArray = byteArrayOutputStream.toByteArray();
                    byteArrayOutputStream.close();
                    return aF.V0(byteArray);
                }
                byte[] doFinal = i3 > 117 ? cipher.doFinal(str2.getBytes(), i, 117) : cipher.doFinal(str2.getBytes(), i, i3);
                byteArrayOutputStream.write(doFinal, 0, doFinal.length);
                i2++;
                i = i2 * 117;
            }
        } catch (Exception unused) {
            return "error";
        }
    }
}
